package com.jiezuo.common.config.flowable;

import com.jiezuo.flowable.listener.GlobalProcessCompletedListener;
import com.jiezuo.flowable.listener.GlobalTaskCompletedListener;
import com.jiezuo.flowable.listener.GlobalTaskCreateListener;
import lombok.RequiredArgsConstructor;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 *@Description Flowable添加全局监听器
 *@Author 庞留杰
 *@Date2021/5/8 14:44
 *@Version V1.0
 **/
@Configuration
@RequiredArgsConstructor //会生成一个包含常量，和标识了NotNull的变量的构造方法。
public class FlowableGlobalListenerConfig implements ApplicationListener<ContextRefreshedEvent> {

    private final SpringProcessEngineConfiguration configuration;

    private final GlobalTaskCreateListener globalTaskCreateListener;

    private final GlobalTaskCompletedListener globalTaskCompletedListener;

    private final GlobalProcessCompletedListener globalProcessCompletedListener;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
        /** 任务创建全局监听-待办消息发送
         *  类型：
         *      TASK_CREATED 添加任务创建全局监听
         *      TASK_COMPLETED 添加任务完成全局监听
         *      PROCESS_STARTED 添加流程实例创建全局监听
         *      PROCESS_COMPLETED 添加流程实例结束全局监听
         */
//        dispatcher.addEventListener(globalTaskCreateListener, FlowableEngineEventType.TASK_CREATED);
        dispatcher.addEventListener(globalTaskCompletedListener, FlowableEngineEventType.TASK_COMPLETED);
        dispatcher.addEventListener(globalProcessCompletedListener, FlowableEngineEventType.PROCESS_COMPLETED);
    }

}